package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.ClockInPeopleLogic;
import cn.topevery.garden.entity.dto.ClockInPeopleDTO;
import cn.topevery.garden.entity.po.ClockInPeople;
import cn.topevery.garden.entity.query.ClockInPeopleQuery;
import cn.topevery.garden.entity.vo.ClockInPeopleVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author code-generate
 * @date 2022-01-12
 */
@Api(tags = "人员打卡记录")
@RestController
@RequestMapping("/clockInPeople")
public class ClockInPeopleController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    ClockInPeopleLogic clockInPeopleLogic;


    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<ClockInPeopleVO>> listPage(@RequestBody ClockInPeopleQuery query) {
        return JsonResult.ok(clockInPeopleLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody ClockInPeopleDTO dto) {
        clockInPeopleLogic.saveOrUpdate(getUser().id.toString(), mapper.map(dto, ClockInPeople.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("补卡")
    @PostMapping("/punchClock")
    public JsonResult<Boolean> punchClock(@Valid @RequestBody ClockInPeopleDTO dto) {
        ClockInPeople entity = clockInPeopleLogic.getEntity(dto.getId());
        entity.setGpsDate(dto.getGpsDate());
        entity.setType((short) 2);
        entity.setStatus((short) 1);
        entity.setAddress(dto.getAddress());
        entity.setUpdateTime(LocalDateTime.now());
        entity.setUpdateId(getUserId());
        clockInPeopleLogic.saveOrUpdate(getUser().id.toString(), entity);
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<ClockInPeopleVO> getInfoById(@RequestParam Integer id) {
        ClockInPeople entity = clockInPeopleLogic.getEntity(id);
        ClockInPeopleVO vo = mapper.map(entity, ClockInPeopleVO.class);
        return JsonResult.ok(vo);
    }

    /**
     * @return
     * @query 片区出勤统计
     * @创建人 dong.zou
     * @创建时间 2022/1/25
     */
    @ApiOperation("片区出勤统计")
    @PostMapping("/getExtentWorkCountList")
    public JsonResult<List<Map<String, Object>>> getExtentWorkCountList(@RequestBody ClockInPeopleQuery query) {
        return JsonResult.ok(clockInPeopleLogic.getExtentWorkCountList(query));
    }

    /**
     * @return
     * @query 人员出勤统计
     * @创建人 dong.zou
     * @创建时间 2022/1/25
     */
    @ApiOperation("人员出勤统计")
    @PostMapping("/getPeopleWorkCountList")
    public JsonResult<List<Map<String, Object>>> getPeopleWorkCountList(@RequestBody ClockInPeopleQuery query) {
        return JsonResult.ok(clockInPeopleLogic.getPeopleWorkCountList(query));
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        clockInPeopleLogic.deleteBatchById(getUser().id.toString(), ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出信息")
    @GetMapping("/export")
    public void export(ClockInPeopleQuery query, HttpServletResponse response) {
        List<ClockInPeopleVO> list = clockInPeopleLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "", "sheet1", ClockInPeopleVO.class, "", response);
    }


}
